package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.*;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
public class DishServiceImpl implements DishService {

    @Autowired
    DishMapper dishMapper;
    @Autowired
    DishFlavorMapper dishFlavorMapper;
    @Autowired
    SetmealDishMapper setmealDishMapper;
    @Autowired
    CategoryMapper categoryMapper;

    @Override
    public List<Dish> getListById(Long categoryId) {
        return dishMapper.getListById(categoryId);
    }

    @Override
    @Transactional
    public void updateDish(DishDTO dto) {

        Dish dish = new Dish();
        BeanUtils.copyProperties(dto,dish);
        dishMapper.update(dish);
        Long dishId = dish.getId();
        ArrayList<Long> list = new ArrayList<>();
        list.add(dishId);
        if(dto.getFlavors()!=null && dto.getFlavors().size()> 0){
            dishFlavorMapper.delete(list);
            List<DishFlavor> result = dto.getFlavors().stream().map(dishFlavor -> {
                dishFlavor.setDishId(dishId);
                return dishFlavor;
            }).collect(Collectors.toList());
            dishFlavorMapper.save(result);
        }

    }

    @Override
    public void update(Integer status, Long id) {
        Dish dish = new Dish();
        dish.setId(id);
        dish.setStatus(status);
        dishMapper.update(dish);
    }

    @Override
    public DishVO getById(Long id) {
        DishVO dishVO = new DishVO();
        Dish dish= dishMapper.getById(id);
       List<DishFlavor> dishFlavor = dishFlavorMapper.getById(id);
       BeanUtils.copyProperties(dish,dishVO);
       dishVO.setFlavors(dishFlavor);
        return dishVO;
    }

    @Override
    @Transactional
    public void delete(List<Long> ids) {
        ids.forEach(
                id -> {
                    Dish dish = dishMapper.getById(id);
                    if (dish.getStatus() == StatusConstant.ENABLE) {
                        throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
                    }
                }
        );
       Integer count = setmealDishMapper.getByIds(ids);
        if(count>0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        dishMapper.delete(ids);
        dishFlavorMapper.delete(ids);
    }

    @Override
    public void save(DishDTO dto) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dto, dish);
        dishMapper.save(dish);
        Long dishId = dish.getId();
        if (dto.getFlavors() != null && dto.getFlavors().size() > 0) {
            List<DishFlavor> collect = dto.getFlavors().stream().map(dishFlavor -> {
                dishFlavor.setDishId(dishId);
                return dishFlavor;
            }).collect(Collectors.toList());
            dishFlavorMapper.save(collect);
        }
    }

    @Override
    public PageResult getPage(DishPageQueryDTO dto) {
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        Page<Dish> dishepage = dishMapper.getPage(dto);
        List<DishVO> result = dishepage.stream().map(dish -> {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(dish, dishVO);
            List<DishFlavor> flavors = dishFlavorMapper.getByDishId(dish.getId());
            dishVO.setFlavors(flavors);
            String cname = categoryMapper.getnameByID(dish.getCategoryId());
            dishVO.setCategoryName(cname);
            return dishVO;
        }).collect(Collectors.toList());
        return PageResult.builder()
                .total(dishepage.getTotal())
                .records(result).build();
    }
}

